import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/animation_dialog.dart';
import 'package:znmm/UI/ProductPages/model/product_model.dart';
import 'package:znmm/UI/ProductPages/show_product_view.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/network/notify_listener.dart';
import '../utils/my_global.dart';
import 'mag/category_mag.dart';
import 'view/category_left_view.dart';
import 'view/category_list_view.dart';

class CategoryPage extends StatefulWidget {
  const CategoryPage({super.key});

  @override
  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
  late List<ProductModel> _pModelList = [];
  int _iNotiId1 = -1;

  @override
  void initState() {
    super.initState();

    CategoryMag().reqProductCategorieGraql();

    _pModelList = CategoryMag().getCategProducList();
    //添加监听者
    _iNotiId1 = NotifyCenter().addObserver(NotifyKey.categProductKey, (object) {
      //print("---刷新---");
      if (mounted) {
        setState(() {
          _pModelList = CategoryMag().getCategProducList();
        });
      }
    });
  }

  @override
  void dispose() {
    NotifyCenter().removeCallback(_iNotiId1);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: AppColor.backgroundColor,
        appBar: MyGlobal.getNavigationBar(context, "Category",
            backWidget: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                showAnimationDialog(
                  context: context,
                  builder: (context) {
                    return CategoryLeftView(
                      onSelCallBack: () {
                        if (mounted) {
                          setState(() {});
                        }
                      },
                    );
                  },
                  transitionType: TransitionType.inFromLeft,
                  barrierDismissible: true,
                );
              },
              child: const Icon(
                Icons.list,
                color: Colors.black,
                size: 25,
              ),
            ),
            actions: [
              IconButton(
                onPressed: () {
                  CategoryMag().reqProductCategorieGraql();
                },
                icon: const Icon(Icons.refresh),
                tooltip: 'refresh',
                color: Colors.black,
              )
            ]),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: const EdgeInsets.fromLTRB(16, 5, 10, 5),
              child: Text(
                "${CategoryMag().categorySlug}  (${_pModelList.length})",
                overflow: TextOverflow.ellipsis,
                style:
                    const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
            ),
            Expanded(
                child: Container(
              color: Colors.white,
              padding: const EdgeInsets.all(5),
              child: _pModelList.isNotEmpty
                  ? CategoryListView(
                      prodcList: _pModelList,
                      onTap: (pModel) {
                        String sulgStr = pModel.slug;
                        print("sulgStr:$sulgStr");
                        CategoryMag().reqProductBySlugGraql(
                          sulgStr,
                          callback: (p0) {
                            ProductModel pmodel = p0;
                            Get.to(ShowProductView(
                              product: pmodel,
                            ));
                          },
                        );
                      },
                    )
                  : MyGlobal.emptyWidget(),
            ))
          ],
        ));
  }
}
